package pl.szpadel.android.gadu;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collection;
import java.util.StringTokenizer;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.util.InetAddressUtils;
import org.apache.http.impl.client.DefaultHttpClient;

import android.content.Context;

/** Stupid simple lib doing the HTPP magic to get the server addresses */
public class HttpServiceConnector {

	private static final String TAG = "AndroidGaduService";
	
	/// Does the thing. Connects to the HTTP service, returns list of addresses
	static Collection<InetSocketAddress> getAddresses(Context ctx, long userId) throws Exception {
		// TODO implement
		AGLog.d(TAG, "Connecting to HTTP service to obtain IP addresses");
    	// build URL
    	String host = (String) ctx.getResources().getText(R.string.gg_webservice_addr);
    	String ver = App.getInstance().getGGClientVersion();
    	String url = host + "?fmnumber=" + Long.toString(userId) + "&lastmsg=0&version="+ver;
    	
    	HttpClient httpClient = new DefaultHttpClient();
    	
    	AGLog.d(TAG, "connecting to http service at " + url);
    	HttpGet request = new HttpGet(url);
    	HttpResponse response = httpClient.execute(request);
    	AGLog.d(TAG, "response status:" + response.getStatusLine().getReasonPhrase());
  
        	// read response
    	HttpEntity ent = response.getEntity();
    	InputStream content = ent.getContent();
    	BufferedReader reader = new BufferedReader(new InputStreamReader(content));
    	if (reader.ready()) {
    		String line = reader.readLine();
    		AGLog.d(TAG, "response: " + line);
    		
    		StringTokenizer tokenizer = new StringTokenizer(line, " ");
    		@SuppressWarnings("unused") String status = tokenizer.nextToken();
    		@SuppressWarnings("unused") String unknown = tokenizer.nextToken();
    		
    		// TODO verify status

    		// parse tokens
    		ArrayList<InetSocketAddress> result = new ArrayList<InetSocketAddress>();
    		while (tokenizer.hasMoreTokens()) {
    			// split string into addr/port portions
    			StringTokenizer addrport = new StringTokenizer(tokenizer.nextToken(), ":");
    			
    			// parse addr
    			String addrStr = addrport.nextToken();
    			if (InetAddressUtils.isIPv4Address(addrStr)){
    				AGLog.d(TAG, "Address decoded successfully: " + addrStr);
    			} else {
    				AGLog.w(TAG, "Failed to decode address: " + addrStr);
    				continue;
    			}
    			
    			// parse port
    			String portStr;
    			if (addrport.hasMoreTokens()) {
    				portStr = addrport.nextToken();
    			} else {
    				portStr = (String) ctx.getResources().getText(R.string.gg_default_port);
    			}
    			AGLog.d(TAG, "Port decoded successfully: " + portStr);
    			short port = Short.decode(portStr);
    			
    			result.add(new InetSocketAddress(addrStr, port));
    		}
    		
    		return result;
    		
    	}
    	else{
    		AGLog.e(TAG, "Service returned no content");
    		throw new ClientProtocolException("Service returned no content");
    	}
	}
	
	
}
